package cn.tqfeiyang.smartadmin.system.service;

import cn.tqfeiyang.smartadmin.system.entity.OperationLogEntity;
import cn.tqfeiyang.smartadmin.system.mapper.OperationLogMapper;
import cn.tqfeiyang.smartadmin.system.request.OperationLogCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.OperationLogUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.OperationLogQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.OperationLogQueryVO;
import cn.tqfeiyang.smartadmin.system.response.OperationLogDetailVO;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.tqfeiyang.smartadmin.commons.exception.BusinessException;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.toolbox.BeanUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.ValidationUtils;

import java.util.List;

/**
 * 操作记录服务
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@Service
public class OperationLogService {

    /**
     * 操作记录映射接口
     */
    @Resource
    private OperationLogMapper operationLogMapper;

    /**
     * 查询操作记录
     *
     * @param dto 请求数据
     * @return 操作记录列表
     */
    public PageData<OperationLogQueryVO> queryOperationLogs(OperationLogQueryDTO dto) {
        ValidationUtils.validateProperties(dto);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<OperationLogEntity> list = operationLogMapper.selectList(dto.getWrapper());
        return PageData.create(list, OperationLogQueryVO.class);
    }

    /**
     * 获取操作记录
     *
     * @param logId 操作记录id
     * @return 操作记录信息
     */
    public OperationLogDetailVO readOperationLog(Long logId) {
        OperationLogEntity operationLogEntity = checkOperationLogExists(logId);
        return BeanUtils.map(operationLogEntity, OperationLogDetailVO.class);
    }

    /**
     * 添加操作记录
     *
     * @param dto 请求数据
     * @return 操作记录id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createOperationLog(OperationLogCreateDTO dto) {
        ValidationUtils.validateProperties(dto);
        OperationLogEntity operationLogEntity = BeanUtils.copy(dto, OperationLogEntity.class);
        operationLogMapper.insert(operationLogEntity);
        return operationLogEntity.getLogId();
    }

    /**
     * 修改操作记录
     *
     * @param logId 操作记录id
     * @param dto 请求数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateOperationLog(Long logId, OperationLogUpdateDTO dto) {
        checkOperationLogExists(logId);
        ValidationUtils.validateProperties(dto);
        OperationLogEntity operationLogEntity = BeanUtils.copy(dto, OperationLogEntity.class);
        operationLogEntity.setLogId(logId);
        operationLogMapper.updateById(operationLogEntity);
    }

    /**
     * 删除操作记录
     *
     * @param logId 操作记录id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteOperationLog(Long logId) {
        checkOperationLogExists(logId);
        operationLogMapper.deleteById(logId);
    }

    /**
     * 检查操作记录实体是否存在
     *
     * @param logId 操作记录id
     * @return 操作记录实体
     */
    protected OperationLogEntity checkOperationLogExists(Long logId) {
        Assert.notNull(logId, "logId不能为空");
        OperationLogEntity operationLogEntity = operationLogMapper.selectById(logId);
        if (ObjectUtils.isEmpty(operationLogEntity)) {
            throw new BusinessException("操作记录不存在");
        }
        return operationLogEntity;
    }

}
